#! /usr/bin/env python
# coding=utf-8
"""
# @copyright: Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
# @Author: y00447189
# @Date: 2020-10-22 16:59:16
# @LastEditTime: 2020-10-22 11:17:42
# @LastEditors: y00447189
# @Description:
# @FilePath: Logger.py
# @可以输入预定的版权声明、个性签名、空行等
"""

import os
import shutil
import logging.config
import yaml

CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = "config"


def init_workspace():
    """
    初始化工作目录
    :return:
    """
    # 初始化路径
    dir_path = os.path.join("", 'getpkgship')
    dir_path = os.path.join(dir_path, 'log')
    if os.path.exists(dir_path):
        shutil.rmtree(dir_path)
    print("init_workspace dir path:", dir_path)
    os.makedirs(dir_path, exist_ok=True)


def init_logger(module):
    """
    初始化日志打印logger
    :return:
    """
    init_workspace()
    conf_dir = os.path.join(CURRENT_PATH, CONFIG_PATH)
    conf_path = os.path.join(conf_dir, 'logConfig.yaml')
    if not os.path.isfile(conf_path):
        print("not found the file:%s.", conf_path)
        return None
    with open(file=conf_path, mode='r', encoding="utf-8") as conf_file:
        logging_yaml = yaml.safe_load(stream=conf_file)
        logging.config.dictConfig(config=logging_yaml)
        print("Init logger succeed.")
        return logging.getLogger(module)
